package com.eeepay.eeepay_v2._tab;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

/**
 * 描述：抽象的BaseFragment 方便使用
 * 作者：zhuangzeqin
 * 时间: 2017/8/30-10:12
 * 邮箱：zzq@eeepay.cn
 */
public abstract class BaseFragment extends Fragment {
    protected Activity mActivity;
    private Toast mToast;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mActivity = (Activity) context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(getLayoutId(), null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view, savedInstanceState);
    }

    public <T extends View> T getViewById(View subView, int id) {
        View view = subView.findViewById(id);
        return (T) view;
    }

    public void showToast(final String text) {
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mToast == null) {
                    mToast = Toast.makeText(mActivity, text, Toast.LENGTH_LONG);
                } else {
                    mToast.setText(text);
                    mToast.setDuration(Toast.LENGTH_SHORT);
                }
                mToast.show();
            }
        });
    }

    protected abstract int getLayoutId();//布局id

    /**
     * 该抽象方法就是 初始化view
     *
     * @param view
     * @param savedInstanceState
     */
    protected abstract void initView(View view, Bundle savedInstanceState);
}
